import { ref,reactive } from 'vue'
import { getLocationData } from '@/axios/api/slot_management/slot_upkeep'
import { addLocation,updateLocation } from '@/axios/api/slot_management/slot_upkeep'
import { useSlotManagementStore } from '@/store/slot_mangement';
import { ElNotification } from 'element-plus';
const SlotManagementStore = useSlotManagementStore();

export default function () {

    let dialog1 = ref(false) // 是否显示新增抽屉
    let dialog2 = ref(false) // 是否显示修改抽屉
    let hook_locationID=ref('')

    let loctionadd_data = reactive({
        LocationID: '',
        MaxPallets: '',
        Note: '',
    })

      // 新增表单校验

    const LocationID = (_rule: any, value: any, callback: any) => {
        // 判断value是否在tableData中
        let flag = false;
        SlotManagementStore.locationtableData_pinia1.forEach((item: any) => {
            if (item.LocationID.toLowerCase() === value.toLowerCase()) {
                flag = true;
                return;
            }
        });

        if (flag) {
            callback(new Error('货位名称已存在'))
        } else {
            callback() //通过
        }
    }

    const MaxPallets = (_rule: any, value: any, callback: any) => {
        if (/^[1-9]\d*$/.test(value)==false) {
            callback(new Error('最大容量只能输入大于0的整数'))
        } else {
            callback() //通过
        }
    }

    const rules = {
        LocationID: [
            { validator: LocationID, trigger: 'change' }
        ],
        MaxPallets: [
            { validator: MaxPallets, trigger: 'change' }
        ],
    }

      // 修改表单校验

      const LocationID1 = (_rule: any, value: any, callback: any) => {
        // 判断value是否在tableData中
        let flag = false;
        SlotManagementStore.locationtableData_pinia1.forEach((item: any) => {
            if (item.LocationID.toLowerCase() === value.toLowerCase()) {
                flag = true;
                return;
            }
        });

        if (flag===false) {
            callback(new Error('货位名称不存在'))
        } else {
            callback() //通过
        }
    }

    const MaxPallets1 = (_rule: any, value: any, callback: any) => {
        if (/^[1-9]\d*$/.test(value)==false) {
            callback(new Error('最大容量只能输入大于0的整数'))
        } else {
            callback() //通过
        }
    }


    const rules1 = {
        LocationID: [
            { validator: LocationID1, trigger: 'change' }
        ],
        MaxPallets: [
            { validator: MaxPallets1, trigger: 'change' }
        ]
    }

    // 获取库位信息

    async function getloction() {

        SlotManagementStore.locationtableData_pinia1=[]
        try {
            let res: any = await getLocationData({locationID:hook_locationID.value})
            SlotManagementStore.locationtableData_pinia1 = JSON.parse(res.data)
            return 'ok';
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    // 新增库位

    async function handdaddLocation() {
        try {
            let res: any = await addLocation({
                LocationID: loctionadd_data.LocationID,
                MaxPallets: loctionadd_data.MaxPallets,
                Note: loctionadd_data.Note,
            })
            if (res.code === 200) {
                ElNotification({
                    title: 'Success',
                    message: '新增库位成功',
                    type:'success',
                })
                await getloction();
                dialog1.value = false;
                return 'ok';
            }else{
                ElNotification({
                    title: 'Error',
                    message: res.msg,
                    type: 'error',
                })
                return Promise.reject(new Error(res.msg))
            }
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }
    }

    // 修改库位
    async function handupdateLocation() {
        try {
            let res: any = await updateLocation({
                LocationID: loctionadd_data.LocationID,
                MaxPallets: loctionadd_data.MaxPallets,
                Note: loctionadd_data.Note,
            })
            if (res.code === 200) {
                ElNotification({
                    title: 'Success',
                    message: '修改库位成功',
                    type:'success',
                })
                await getloction();
                dialog2.value = false;
                return 'ok';
            }else{
                ElNotification({
                    title: 'Error',
                    message: res.msg,
                    type: 'error',
                })
                return Promise.reject(new Error(res.msg))
            }
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
            return Promise.reject(new Error(error))
        }

    }



    return {
        dialog1,
        dialog2,
        loctionadd_data,
        rules,
        rules1,
        getloction,
        updateLocation,
        handdaddLocation,
        handupdateLocation,
        hook_locationID
    }
}